package com.atguigu.system.utils;


import com.alibaba.fastjson.JSONObject;
import com.atguigu.model.system.SysCalendar;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author quzengpeng
 * @descpription: 组装日历数据
 * @date 2024/6/19
 */
public class HolidayChecker {

    /**
     * 法定节假日地址
     */
    static final String HOLIDAY_PATH = "https://timor.tech/api/holiday/year/";

    static Calendar calendar = Calendar.getInstance();

    static Integer year = calendar.get(Calendar.YEAR);
    static Integer month = calendar.get(Calendar.MONTH) + 1;


    static DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    static List<SysCalendar> dataList = new ArrayList<>();

    public static void main(String[] args) {
        getDataList(month);
    }

    public static List<SysCalendar> getDataList(Integer month){

        //某月一共有多少天
        int daysInMonth = getMonthDay(year,month);
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= daysInMonth; i++) {
            String temp = year + "-"
                    + (month < 10 ? "0" + month : month) + "-"
                    + (i < 10 ? "0" + i : "" + i);
            list.add(temp);
        }
        //获取节假日
        Map<String, Object> map = getHoliday(year, month);

        for (String item : list) {
            SysCalendar sysCalendar = new SysCalendar();
            sysCalendar.setSysDate(item);
            String week = week(item);
            sysCalendar.setSysWeek(week);
            JSONObject obj = (JSONObject) map.get(item);
            sysCalendar.setIsHoliday(obj != null ? "1": "0");
            if (obj !=null){
                String holiday = (String) obj.get("name");
                sysCalendar.setHoliday(holiday);
            }
            sysCalendar.setIsWeekend((week.equals("六") || week.equals("日")) ? "1": "0");
            sysCalendar.setDay(item.split("-")[2]);
            sysCalendar.setDay2(LunarCalendar.getDay(item));
            sysCalendar.setSysMonth(String.valueOf(month));
            dataList.add(sysCalendar);
        }

        System.out.println(JSONObject.toJSONString(dataList));
        return dataList;
    }

    /**
     * 周几
     * @param dateStr
     * @return
     */
    public static String week(String dateStr) {
        LocalDate localDate = LocalDate.parse(dateStr, df);
        DayOfWeek week = localDate.getDayOfWeek();
        int value = week.getValue();
        String result = "";
        switch (value) {
            case 1:
                result = "一";
                break;
            case 2:
                result = "二";
                break;
            case 3:
                result = "三";
                break;
            case 4:
                result = "四";
                break;
            case 5:
                result = "五";
                break;
            case 6:
                result = "六";
                break;
            case 7:
                result = "日";
                break;
        }
        return result;
    }

    /**
     * 获取某月一共有多少天
     * @return
     */
    public static int getMonthDay(Integer year,Integer month){
        calendar.set(year,month - 1, 1);
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return daysInMonth;
    }

    /**
     * 获取节假日不含周末
     * @param year 年份
     * @param month 月份
     * @return
     */
    private static Map getHoliday(int year, int month) {
        String url = HOLIDAY_PATH + year + "-" + month;
        // 创建HttpClient实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result = null;
        try {
            // 创建HttpGet实例，设置第三方API的URL
            HttpGet httpGet = new HttpGet(url);
            // 执行请求并获得响应
            HttpResponse response = httpClient.execute(httpGet);
            // 从响应对象中提取字符串
            result = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                // 关闭HttpClient实例
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Map map = JSONObject.parseObject(result, Map.class);
        map.remove("code");
        Map hashMap = new HashMap<>();
        JSONObject holiday = (JSONObject) map.get("holiday");
        for (String key : holiday.keySet()){
            hashMap.put(((JSONObject)holiday.get(key)).get("date"),holiday.get(key));
        }
        return hashMap;
    }
}
